﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Quartz;
using ZYing.QPanel.Constants;

namespace ZYing.QPanel.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ZYing.QPanel.Controls"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ZYing.QPanel.Controls;assembly=ZYing.QPanel.Controls"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:ZStateIcon/>
    ///
    /// </summary>
    public class ZStateIcon : Control
    {
        static ZStateIcon()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZStateIcon), new FrameworkPropertyMetadata(typeof(ZStateIcon)));
        }
        public ZStateIcon()
        {

        }

        public MahApps.Metro.IconPacks.PackIconFontAwesome Icon { get; } = new MahApps.Metro.IconPacks.PackIconFontAwesome
        {
            Visibility = Visibility.Collapsed,
            FontSize = 9,
            Width = 11,
            Height = 11,
            //HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            HorizontalContentAlignment = HorizontalAlignment.Center,
            VerticalContentAlignment = VerticalAlignment.Center,
            Margin = new Thickness(0, 0, 4, 0)
        };
        private void SetRunning()
        {
            Icon.Visibility = Visibility.Visible;
            Icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.CircleSolid;
            Icon.Foreground = BrushConstants.NotPaused;
        }
        private void SetPaused()
        {

            Icon.Visibility = Visibility.Visible;
            Icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.PauseSolid;
            Icon.Foreground = BrushConstants.Paused;
        }
        private void SetNull()
        {
            Icon.Visibility = Visibility.Collapsed;
        }
        public bool? IsPaused
        {
            get { return (bool?)GetValue(IsPausedProperty); }
            set { SetValue(IsPausedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsPaused.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsPausedProperty =
            DependencyProperty.Register("IsPaused", typeof(bool?), typeof(ZStateIcon), new PropertyMetadata(null, IsPausedChanged));

        private static void IsPausedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ZStateIcon icon)) return;
            switch (e.NewValue)
            {
                case bool isPaused:
                    if (isPaused)
                    {
                        icon.SetPaused();
                    }
                    else
                    {
                        icon.SetRunning();
                    }
                    break;
                default:
                    icon.SetNull();
                    break;
            }
        }

        public TriggerState? State
        {
            get { return (TriggerState?)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for State.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(TriggerState?), typeof(ZStateIcon), new PropertyMetadata(null, StateChanged));

        private static void StateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ZStateIcon icon)) return;
            switch (e.NewValue)
            {
                case TriggerState state:
                    switch (state)
                    {
                        case TriggerState.Paused:
                            icon.SetPaused();
                            break;
                        default:
                            icon.SetRunning();
                            break;
                    }
                    break;
                default:
                    icon.SetNull();
                    break;
            }
        }


        public JobState? JobState
        {
            get { return (JobState?)GetValue(JobStateProperty); }
            set { SetValue(JobStateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for JobState.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty JobStateProperty =
            DependencyProperty.Register("JobState", typeof(JobState?), typeof(ZStateIcon), new PropertyMetadata(null, JobStateChanged));

        private static void JobStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ZStateIcon icon)) return;
            switch (e.NewValue)
            {
                case JobState state:
                    switch (state)
                    {
                        case QPanel.JobState.Running:
                            icon.Icon.Visibility = Visibility.Visible;
                            icon.Icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.PlaySolid;
                            icon.Icon.Foreground = BrushConstants.Running;
                            break;
                        case QPanel.JobState.Complated:
                            icon.Icon.Visibility = Visibility.Visible;
                            icon.Icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.CheckSolid;
                            icon.Icon.Foreground = BrushConstants.Complated;
                            break;
                        case QPanel.JobState.Fail:
                            icon.Icon.Visibility = Visibility.Visible;
                            icon.Icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.TimesSolid;
                            icon.Icon.Foreground = BrushConstants.Fail;
                            break;
                        default:
                            icon.SetNull();
                            break;
                    }
                    break;
                default:
                    icon.SetNull();
                    break;
            }
        }
    }
}
